/**
 * 
 */
package cz.geoserver.KMLpublisher.db.model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author mkepka
 *
 */
public class UnitPosition {
	
	private final int gid;
	private final long unit_id;
	private Date time_stamp;
	private final String time_string;
	private final double speed;
	private final double x;
	private final double y;
	private final String geomString;
	
	List<Attribute> attsList;
	
	private final SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");

	public UnitPosition(){
		speed = 0;
		unit_id = 0;
		gid = 0;
		y = 0;
		x = 0;
		time_string = null;
		geomString = null;
	}
	
	public UnitPosition(ResultSet set) throws SQLException {
		attsList = new LinkedList<Attribute>();
		this.gid = set.getInt("gid");
		attsList.add(new Attribute("gid", String.valueOf(gid)));
		this.unit_id = set.getLong("unit_id");
		attsList.add(new Attribute("unit_id", String.valueOf(unit_id)));
		this.time_string = set.getString("time_stamp");
		attsList.add(new Attribute("time_stamp", time_string));
		this.speed = set.getDouble("speed");
		attsList.add(new Attribute("speed", String.valueOf(speed)));
		this.x = set.getDouble("x");
		attsList.add(new Attribute("x", String.valueOf(x)));
		this.y = set.getDouble("y");
		attsList.add(new Attribute("y", String.valueOf(y)));
		this.geomString = set.getString("asText");
	}

	public String getGeomString() {
		return geomString;
	}

	public List<Attribute> getAttibutes() {
		return attsList;
	}

	/**
	 * @return the time_stamp
	 */
	public Date getTime_stamp() {
		return time_stamp;
	}

	/**
	 * @return the gid
	 */
	public int getGid() {
		return gid;
	}

	/**
	 * @return the unit_id
	 */
	public long getUnit_id() {
		return unit_id;
	}

	/**
	 * @return the time_string
	 */
	public String getTime_string() {
		return time_string;
	}

	/**
	 * @return the speed
	 */
	public double getSpeed() {
		return speed;
	}

	/**
	 * @return the x
	 */
	public double getX() {
		return x;
	}

	/**
	 * @return the y
	 */
	public double getY() {
		return y;
	}
	
	/**
	 * Method returns time stamp of position as Date
	 * @return timestamp as Date
	 */
	public Date internalGetTime_stamp() {
		if (time_stamp == null) {
			try {
				time_stamp = formater.parse(time_string + "00");
			} catch (ParseException e) {
				// Should never happpend
				e.printStackTrace();
			}
		}
		return time_stamp;
	}
	
	public String toString(){
		return "["+getGeomString()+", "+getAttibutes()+"]";
	}
	
	public String getKMLPoint(){
		return "<coordinates>"+getX()+", "+getY()+"</coordinates>";
	}
	
	/**
	 * 
	 * @return
	 */
	public String getKMLDescription(){
		String desc = "<![CDATA[" +
				"<TABLE FRAME=BOX RULES=ALL>" +
				"<TR><TH>GID</TH><TH>TIME STAMP</TH><TH>LON</TH><TH>LAT</TH><TH>SPEED</TH><TH>UNIT ID</TH></TR>" +
				"<TR><TD>"+getGid()+"</TD><TD>"+getTime_string()+"</TD><TD>"+getX()+"</TD><TD>"+getY()+"</TD><TD>"+getSpeed()+"</TD><TD>"+getUnit_id()+"</TD></TR>" +
				"</TABLE>" +
				"]]>";
		return desc;
	}
}
